//
//  ClassifyController.m
//  Cook
//
//  Created by Macbookpro on 15/4/14.
//  Copyright (c) 2015年 dingjun. All rights reserved.
//

#import "ClassifyController.h"
#import "CellModel.h"
#import "HomePageViewController.h"
#import "FoodHealthViewController.h"
#import "CapacitySearchViewController.h"
#import "MySecondPageViewController.h"
#import "SecondController.h"

@interface ClassifyController ()<UITableViewDataSource, UITableViewDelegate, UITabBarControllerDelegate>
{
    CGAdapter _myAd;
    MySecondPageViewController *_fourth;
    
    
}
@property (nonatomic, retain) NSMutableArray *tableArray;

@end

@implementation ClassifyController
- (void)dealloc
{
    [_tableArray release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        [_tableArray addObject:@"养生素菜"];
        [_tableArray addObject:@"营养荤菜"];
        [_tableArray addObject:@"河鲜海鲜"];
        [_tableArray addObject:@"家常小炒"];
        [_tableArray addObject:@"汤汤水水"];
        [_tableArray addObject:@"主食副食"];
        [_tableArray addObject:@"八大菜系"];
        [_tableArray addObject:@"烘培小点"];
        [_tableArray addObject:@"生鲜蔬果汁"];
        [_tableArray addObject:@"营养豆浆"];
        
        _myAd = [AdapterModel getCGAdapter];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatBackGround];
    [self creatTableView];
    [self creatTab];
    
   
}

- (void)creatBackGround
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [imageView setImage:[UIImage imageNamed:@"classifyPhoto1.png"]];
    [self.view addSubview:imageView];
    [imageView release];
    
}

- (void)creatTableView
{
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 50 * _myAd.sHeight, 220 * _myAd.sWidth, self.view.frame.size.height - 50 * _myAd.sHeight) style:UITableViewStylePlain];
    [self.view addSubview:table];
    [self.view setBackgroundColor:[UIColor clearColor]];
    [table setBackgroundColor:[UIColor clearColor]];
    
    table.scrollEnabled = NO;
    table.dataSource = self;
    table.delegate = self;
    
    [table setRowHeight:59 * _myAd.sHeight];
    [table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [table release];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"menu";
    CellModel *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[CellModel alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify] autorelease];
    }
    cell.label.text = [_tableArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.label.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    SecondController *second = [[SecondController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:second];
    second.arrayNum = indexPath.row;
    second.titleName = [_tableArray objectAtIndex:indexPath.row];
    [self presentViewController:nav animated:YES completion:nil];
    [second release];
}

- (void)creatTab
{
    UIImage *aImage = [UIImage imageNamed:@"Home1.png"];
    UIImage *bImage = [UIImage imageNamed:@"Home2.png"];
    UIImage *cImage = [UIImage imageNamed:@"Home3.png"];
    UIImage *dImage = [UIImage imageNamed:@"Home4.png"];
    
    HomePageViewController *first = [[HomePageViewController alloc] init];
    first.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:aImage selectedImage:nil];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:first];
    
    
    FoodHealthViewController *second = [[FoodHealthViewController alloc] init];
    second.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"对症食疗" image:bImage selectedImage:nil];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:second];
    
    
    CapacitySearchViewController *third = [[CapacitySearchViewController alloc] init];
    third.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"智能搜索" image:cImage selectedImage:nil];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:third];
   
    
    _fourth = [[MySecondPageViewController alloc] init];
    _fourth.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:dImage selectedImage:nil];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:_fourth];
    nav4.tabBarItem.tag = 103;
    
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:nav1];
    [array addObject:nav2];
    [array addObject:nav3];
    [array addObject:nav4];
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.delegate = self;
    tab.viewControllers = array;
    nav1.tabBarController.tabBar.tintColor = [UIColor blackColor];
    //    first.tabBarController.tabBar.barTintColor = [UIColor blackColor];
    //    self.tabBarController.tabBar.barTintColor = [UIColor yellowColor];
    
    [self addChildViewController:tab];
    [self.view addSubview:tab.view];
    
    tab.tabBar.barTintColor = [UIColor whiteColor];
    
    [nav1 release];
    [nav2 release];
    [nav3 release];
    [nav4 release];
    [first release];
    [second release];
    [third release];
    [_fourth release];
    [first.tabBarItem release];
    [second.tabBarItem release];
    [third.tabBarItem release];
    [_fourth.tabBarItem release];
    [tab release];
    
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if(viewController.tabBarItem.tag == 103){
        [_fourth setDataSource];
    }
}

//- (void)creatTabController
//{
//    TabBarController *first = [[TabBarController alloc] init];
//    first.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//    [self addChildViewController:first];
//    [self.view addSubview:first.view];
//    [first release];
//}

@end
